var express = require('express');
var router = express.Router();
var sql = require('../db/sql');

/* GET users listing. */
// router.get('/', function(req, res, next) {
//   res.send('respond with a api');
// });

// 用户信息登入
router.post('/users', function (req, res, next) {
  console.log(req.body);
  let bod = req.body;
  sql(`select * from user where username = '${bod.username}' and password = '${bod.password}'`).then(data => {
    // console.log(data);
    if (data.length > 0) {
      
        // 数据登入成功，再去请求cart数据
        sql(`select * from user where username = '${bod.username}'`).then(data => {
          res.send({
            code: 200,
            msg: "登入成功",
            shop:data
        })

      })
    } else {
      res.send({
        code: 404,
        msg: "登入失败"
      })
    }
  }).catch(err => {
    console.log(err);
  })
});

// 用户信息注册
router.post("/register", function (req, res, next) {
  // 查看获取前端的值
  // console.log(req.body);
  var users = req.body;
  // 数据库查找
  sql('select * from user').then(data => {
    // 匹配前端页面的值与数据库的值是否相同，返回true与false
    var useron = data.some(item => {
      return item.username == users.username;
    })
    // 当useron为true时，表示数据库已经有这个数据就不能注册了
    if (useron) {
      res.send({
        code: 400,
        msg: "此账号已注册，请重新注册"
      })
      // 当为false时,把数据存储在数据库中，并成功则发送注册成功
    } else {
      sql(`insert into user value('${users.username}','${users.password}','${users.radio}','${users.tel}','${users.cart = null}')`).then(result => {
        console.log(result);
        if (result) {
          res.send({
            code: 200,
            msg: "注册成功"
          })
        }
      })
    }
  })

})

//  homes里图片加载
router.get("/guessarr", (req, res, next) => {
  sql("select * from homesguessarr").then(data => {
    res.send(data);
  })
})
router.get("/homestodayarr1", (req, res, next) => {
  sql("select * from homestodayarr1").then(data => {
    res.send(data);
  })
})
router.get("/homestodayarr2", (req, res, next) => {
  sql("select * from homestodayarr2").then(data => {
    res.send(data);
  })
})
router.get("/televsetarr", (req, res, next) => {
  sql("select * from televsetarr").then(data => {
    res.send(data);
  })

})

// 退出页面
router.post("/cart", (req, res, next) => {
  // console.log(req.body.username);
  var video = JSON.stringify(req.body.video)
  
  var username = req.body.username
  console.log(username + "-------------");
  sql(`select * from user where username = '${username}'`).then(data => {
    // console.log(data);
    if (data.length == 0) {
      sql(`insert into user(username, cart) values('${username}',"{'video':${video}}")`).then(data => {
        // console.log(12345);
        if (data) {
          res.send({
            code: 200,
            msg: "浏览记录保存成功"
          })
        } else {
          res.send({
            code: 400,
            msg: "浏览记录保存失败"
          })
        }
      })
    } else {
      console.log(909099);
      sql(`update user set cart = '{"video":${video}}' where username = '${username}' `).then(data => {
        // console.log(data);
        if (data) {
          res.send({
            code: 200,
            msg: "浏览记录保存成功"
          })
        } else {
          res.send({
            code: 400,
            msg: "浏览记录保存失败"
          })
        }
      })
    }
  })



})

// homes页面

// navarr接口
router.get("/succeednavarr", (req, res, next) => {
  sql("select * from succeednavarr").then(data => {
    res.send(data);
  })
})

// boxarr接口
router.get("/succeed", (req, res, next) => {
  sql("select * from succeedboxarr").then(data => {
    // res.send(data);
  })
})


// homes页面

// homepage接口
router.get("/homepageimgarr", (req, res, next) => {
  sql("select * from homepageimgarr").then(data => {
    res.send(data)
    // console.log(data);
  })
})


// homePage组件接口
router.get("/homePagelbarr", (req, res, next) => {
  sql("select * from homepagelbarr").then(data => {
    res.send(data)
    // console.log(data);
  })
})


// group组件接口
router.get("/tvtelevisionarr1", (req, res, next) => {
  sql("select * from grouptvtelevisionarr1").then(data => {
    res.send(data)
  })
})


// group组件接口
router.get("/tvtelevisionarr2", (req, res, next) => {
  sql("select * from tvtelevisionarr2").then(data => {
    res.send(data)
  })
})

// group组件接口
router.get("/dytelevisionarr1", (req, res, next) => {
  sql("select * from dytelevisionarr1").then(data => {
    res.send(data)
    // console.log(data);
  })
})


// group组件接口
router.get("/dytelevisionarr2", (req, res, next) => {
  sql("select * from dytelevisionarr2").then(data => {
    res.send(data)
  })
})


// group组件接口
router.get("/dmtelevisionarr1", (req, res, next) => {
  sql("select * from dmtelevisionarr1").then(data => {
    res.send(data)
  })
})



// group组件接口
router.get("/dmtelevisionarr2", (req, res, next) => {
  sql("select * from dmtelevisionarr2").then(data => {
    res.send(data)
  })
})


// circum组件接口
router.get("/rim", (req, res, next) => {
  sql("select * from circumrim").then(data => {
    res.send(data)
  })
})


// classify组件接口
router.get("/fication", (req, res, next) => {
  sql("select * from classifyfication").then(data => {
    res.send(data)
    console.log(data);
  })
})

// minecomm组件接口
router.get("/minecommboxarr", (req, res, next) => {
  sql("select * from minecommboxarr").then(data => {
    res.send(data)
  })
})

// minecomm组件接口
router.get("/minecommbox3", (req, res, next) => {
  sql("select * from minecommbox3").then(data => {
    res.send(data)
  })
})






module.exports = router;
